// @flow
import React, { Component } from 'react'
import { Button } from 'antd'
import { AgGridReact } from 'ag-grid-react'
import { TableResize } from 'ag-grid-addons'
import JobFileActions from './JobFileActions'
import styles from './fileGrid.css'
type Props = {
  rowData: Array<Object>,
  history: Object
}
type States = {
  columnDefs: Array<Object>
}
class JobListGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  TableResize: Function
  goBack: Function
  api: Object
  constructor (props: Props) {
    super(props)
    this.state = {
      columnDefs:  [
        { headerName: '序号',
          field: 'key',
          width: 80,
          minWidth: 80,
          maxWidth: 80,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        { headerName: '名称',
          field: 'name',
          cellStyle: {
            padding: '8px'
          },
          cellRendererParams: {
            history: this.props.history
          },
          cellRendererFramework: JobFileActions
        },
        { headerName: '大小',
          field: 'size',
          width: 80,
          minWidth: 80,
          maxWidth: 80,
          cellStyle: {
            padding: '8px'
          }
        },
        { headerName: '类型',
          field: 'type',
          width: 80,
          minWidth: 80,
          maxWidth: 80,
          cellStyle: {
            padding: '8px'
          }
        }
      ]
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
    this.goBack = this.goBack.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }
  goBack () {
    // this.props.history.push('/analysis/jobs')
    history.go(-1)
  }
  render () {
    const { columnDefs } = this.state
    let { rowData } = this.props
    return (
      <div className={styles['main']}>
        <Button className={styles['goBack']} onClick={this.goBack}>返回</Button>
        <div className={styles['grid'] + ' ag-fresh'}>
          <AgGridReact {...{
            gridOptions: this.gridOptions,
            rowHeight: '34',
            headerHeight: '28',
            columnDefs: columnDefs,
            rowData: rowData.map((d, i) => {
              d['key'] = i + 1
              return d
            }),
            onGridReady: this.onGridReady
          }}
            />
        </div>
      </div>
    )
  }
}

export default JobListGrid
